﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ProductStock_StockStatistics : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Btn_Search.Attributes.Add("onclick", "return spsearch();");
            AspNetPager1.PageSize = int.Parse(ConfigurationManager.AppSettings["RowCountPerPage"]);
            Bind_Brand();
            bind_KW();
            Bind_GV();
        }
    }


    #region 绑定GV
    private void Bind_GV()
    {
        string where = GetSql();
        
        DataTable dt = new DataTable();
        string by = " spkcb.spdm ";

        if (CKB_GG.Checked)
        {
            AspNetPager1.RecordCount = int.Parse(new BLL.SPKCB().OC_Product_StockCount(where, "1"));
            dt = new BLL.SPKCB().OC_Product_StockListBywhere(AspNetPager1.StartRecordIndex, AspNetPager1.PageSize, by, where, 1, GetQuantitySqlStringOfGG());
        }
        else
        {
            AspNetPager1.RecordCount = int.Parse(new BLL.SPKCB().OC_Product_StockCount(where, "0"));
            dt = new BLL.SPKCB().OC_Product_StockListBywhere(AspNetPager1.StartRecordIndex, AspNetPager1.PageSize, by, where, 0, GetQuantitySqlString());
        }
        if (!CKB_GG.Checked)
        {
            GridView1.Columns[1].Visible = false;
            GridView1.Columns[2].Visible = false;
        }
        else
        {
            GridView1.Columns[1].Visible = true;
            GridView1.Columns[2].Visible = true;
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
    #endregion

    #region 查询字符串

    private string GetSql()
    {
        string where = " spkcb.spdm=shangpin.spdm ";
        if (ddl_Brnad.SelectedIndex > 0)
        {
            where = where + " and shangpin.byzd3='" + ddl_Brnad.SelectedValue + "'";
        }

        //if (DDL_KW.SelectedIndex > 0)
        //{
        //    where = where + " and spkcb.KWDM='" + DDL_KW.SelectedValue + "'";
        //}
        if (HiddenField1.Value.Trim().Length > 0)
        {
            where += " and ( ";
            string[] sparr = HiddenField1.Value.Trim().Split(',');
            for (int a = 0; a < sparr.Length; a++)
            {
                where += " spkcb.spdm='" + sparr[a] + "' ";
                where += " or";
            }
            where = where.Substring(0, where.Length - 2);
            where += " ) ";
        }
        //if (CKB_GG.Checked)
        //{
        //    if (ddl_color.SelectedIndex > 0)
        //    {
        //        where = where + " and OC_Product_Stocks.GG1DM='" + ddl_color.SelectedValue + "'";
        //    }
        //    if (ddl_Size.SelectedIndex > 0)
        //    {
        //        where = where + " and OC_Product_Stocks.GG2DM='" + ddl_Size.SelectedValue + "'";
        //    }
        //}
        return where;
        
    }

    private string GetQuantitySqlString()
    {
        try
        {
            string where = "";

            #region 在库数量
            if (txtInStockQuantityMin.Text != "" && txtInStockQuantityMax.Text != "")
            {
                where += " and #indextable.S_Sl>=" + Convert.ToInt32(txtInStockQuantityMin.Text.Trim()).ToString();
                where += " and #indextable.S_Sl<=" + Convert.ToInt32(txtInStockQuantityMax.Text.Trim()).ToString();
            }
            else if (txtInStockQuantityMin.Text != "" && txtInStockQuantityMax.Text == "")
            {
                where += " and #indextable.S_Sl>=" + Convert.ToInt32(txtInStockQuantityMin.Text.Trim()).ToString();
            }
            else if (txtInStockQuantityMin.Text == "" && txtInStockQuantityMax.Text != "")
            {
                where += " and #indextable.S_Sl<=" + Convert.ToInt32(txtInStockQuantityMax.Text.Trim()).ToString();
            }
            #endregion

            #region 占用数量
            if (txtInUseQuantityMin.Text != "" && txtInUseQuantityMax.Text != "")
            {
                where += " and S_SL-S_Kysl>=" + Convert.ToInt32(txtInUseQuantityMin.Text.Trim()).ToString();
                where += " and S_SL-S_Kysl<=" + Convert.ToInt32(txtInUseQuantityMax.Text.Trim()).ToString();
            }
            else if (txtInUseQuantityMin.Text != "" && txtInUseQuantityMax.Text == "")
            {
                where += " and S_SL-S_Kysl>=" + Convert.ToInt32(txtInUseQuantityMin.Text.Trim()).ToString();
            }
            else if (txtInUseQuantityMin.Text == "" && txtInUseQuantityMax.Text != "")
            {
                where += " and S_SL-S_Kysl<=" + Convert.ToInt32(txtInUseQuantityMax.Text.Trim()).ToString();
            }

            #endregion

            #region 可用数量
            if (txtUsableQuantityMin.Text != "" && txtUsableQuantityMax.Text != "")
            {
                where += " and S_Kysl>=" + Convert.ToInt32(txtUsableQuantityMin.Text.Trim()).ToString();
                where += " and S_Kysl<=" + Convert.ToInt32(txtUsableQuantityMax.Text.Trim()).ToString();
            }
            else if (txtUsableQuantityMin.Text != "" && txtUsableQuantityMax.Text == "")
            {
                where += " and S_Kysl>=" + Convert.ToInt32(txtUsableQuantityMin.Text.Trim()).ToString();
            }
            else if (txtUsableQuantityMin.Text == "" && txtUsableQuantityMax.Text != "")
            {
                where += " and S_Kysl<=" + Convert.ToInt32(txtUsableQuantityMax.Text.Trim()).ToString();
            }
            #endregion
            
            return where;
            //在库数量 S_SL
            //占用数量 S_Zy
            //可用数量 S_Kysl
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page,"请输入正确的数量，输入的数量必须是整数！");
            return "";
        }
        

    }


    private string GetQuantitySqlStringOfGG()
    {
        try
        {
            string where = "";

            #region 在库数量
            if (txtInStockQuantityMin.Text != "" && txtInStockQuantityMax.Text != "")
            {
                where += " and #table.S_Sl>=" + Convert.ToInt32(txtInStockQuantityMin.Text.Trim()).ToString();
                where += " and #table.S_Sl<=" + Convert.ToInt32(txtInStockQuantityMax.Text.Trim()).ToString();
            }
            else if (txtInStockQuantityMin.Text != "" && txtInStockQuantityMax.Text == "")
            {
                where += " and #table.S_Sl>=" + Convert.ToInt32(txtInStockQuantityMin.Text.Trim()).ToString();
            }
            else if (txtInStockQuantityMin.Text == "" && txtInStockQuantityMax.Text != "")
            {
                where += " and #table.S_Sl<=" + Convert.ToInt32(txtInStockQuantityMax.Text.Trim()).ToString();
            }
            #endregion

            #region 占用数量
            if (txtInUseQuantityMin.Text != "" && txtInUseQuantityMax.Text != "")
            {
                where += " and S_SL-S_Kysl>=" + Convert.ToInt32(txtInUseQuantityMin.Text.Trim()).ToString();
                where += " and S_SL-S_Kysl<=" + Convert.ToInt32(txtInUseQuantityMax.Text.Trim()).ToString();
            }
            else if (txtInUseQuantityMin.Text != "" && txtInUseQuantityMax.Text == "")
            {
                where += " and S_SL-S_Kysl>=" + Convert.ToInt32(txtInUseQuantityMin.Text.Trim()).ToString();
            }
            else if (txtInUseQuantityMin.Text == "" && txtInUseQuantityMax.Text != "")
            {
                where += " and S_SL-S_Kysl<=" + Convert.ToInt32(txtInUseQuantityMax.Text.Trim()).ToString();
            }

            #endregion

            #region 可用数量
            if (txtUsableQuantityMin.Text != "" && txtUsableQuantityMax.Text != "")
            {
                where += " and S_Kysl>=" + Convert.ToInt32(txtUsableQuantityMin.Text.Trim()).ToString();
                where += " and S_Kysl<=" + Convert.ToInt32(txtUsableQuantityMax.Text.Trim()).ToString();
            }
            else if (txtUsableQuantityMin.Text != "" && txtUsableQuantityMax.Text == "")
            {
                where += " and S_Kysl>=" + Convert.ToInt32(txtUsableQuantityMin.Text.Trim()).ToString();
            }
            else if (txtUsableQuantityMin.Text == "" && txtUsableQuantityMax.Text != "")
            {
                where += " and S_Kysl<=" + Convert.ToInt32(txtUsableQuantityMax.Text.Trim()).ToString();
            }
            #endregion

            return where;
            //在库数量 S_SL
            //占用数量 S_Zy
            //可用数量 S_Kysl
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, "请输入正确的数量，输入的数量必须是整数！");
            return "";
        }


    }

    #endregion

    #region 品牌
    private void Bind_Brand()
    {
        int pageCount, rowCount;
        ddl_Brnad.DataSource = new BLL.PINPAI().GetList(100, 1, "ppdm", false, "", out pageCount, out rowCount);
        ddl_Brnad.DataTextField = "PPMC";
        ddl_Brnad.DataValueField = "PPDM";
        ddl_Brnad.DataBind();
    }
    #endregion


    #region 查找
    protected void Btn_Search_Click(object sender, EventArgs e)
    {
        Bind_GV();
    }
    #endregion


    #region 翻页
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind_GV();
    }
    #endregion

    #region 绑定库位
    private void bind_KW()
    {
        DDL_KW.DataSource = new BLL.SPKCB().OC_Product_Stocks_KWDM();
        DDL_KW.DataTextField = "KWDM";
        DDL_KW.DataValueField = "KWDM";
        DDL_KW.DataBind();
    }
    #endregion


    protected void Tbx_DM_TextChanged(object sender, EventArgs e)
    {
        HiddenField1.Value = Tbx_DM.Text.Trim();
        Bind_GV();
    }
}
